/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.thienloc.omap.controller.broadcast;

import com.thienloc.omap.controller.AbstractCrudBean;
import com.thienloc.omap.entity.BroadcastConfig;
import com.thienloc.omap.entity.ModuleConfig;
import com.thienloc.omap.session.ModuleConfigFacade;
import com.thienloc.omap.util.Constant;
import com.thienloc.omap.util.JsfUtil;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

/**
 *
 * @author GiangLT
 */
@ManagedBean
@ViewScoped
public class BroadcastConfigBean extends AbstractCrudBean {

    @EJB
    private ModuleConfigFacade facade;
    private BroadcastConfig selected;

    @Override
    protected void validateForm() throws Exception {
    }

    @Override
    protected Logger getLogger() {
        return LoggerFactory.getLogger(BroadcastConfigBean.class);
    }

    @Override
    protected void resetToCreateFormAction() throws Exception {
        List<ModuleConfig> broadcastConfigs = facade.find(Constant.BROADCAST_MODULE_NAME);
        selected = new BroadcastConfig();
        for (ModuleConfig moduleConfig : broadcastConfigs) {
            if (moduleConfig.getConfigKey().equalsIgnoreCase(BroadcastConfig.OPERATOR_KEY)) {
                selected.setOperator(moduleConfig.getConfigValue());
            } else if (moduleConfig.getConfigKey().equalsIgnoreCase(BroadcastConfig.MAX_REQUEST_NOT_COMPLETED_KEY)) {
                selected.setMaxRequestNotCompleted(Integer.valueOf(moduleConfig.getConfigValue()));
            } else if (moduleConfig.getConfigKey().equalsIgnoreCase(BroadcastConfig.SHORTCODE_KEY)) {
                selected.setShortCode(moduleConfig.getConfigValue());
            } else if (moduleConfig.getConfigKey().equalsIgnoreCase(BroadcastConfig.SPEED_KEY)) {
                selected.setSpeed(Integer.valueOf(moduleConfig.getConfigValue()));
            }
        }
        edit = false;
        view = false;
    }

    @Override
    protected void findSelected(int id) throws Exception {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    protected void createSelected() throws Exception {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    protected void editSelected() throws Exception {
        ModuleConfig moduleConfig;
        moduleConfig = facade.findByModuleAndKey(Constant.BROADCAST_MODULE_NAME, BroadcastConfig.MAX_REQUEST_NOT_COMPLETED_KEY);
        moduleConfig.setConfigValue(String.valueOf(selected.getMaxRequestNotCompleted()));
        facade.edit(moduleConfig);

        moduleConfig = facade.findByModuleAndKey(Constant.BROADCAST_MODULE_NAME, BroadcastConfig.OPERATOR_KEY);
        moduleConfig.setConfigValue(selected.getOperator());
        facade.edit(moduleConfig);

        moduleConfig = facade.findByModuleAndKey(Constant.BROADCAST_MODULE_NAME, BroadcastConfig.SHORTCODE_KEY);
        moduleConfig.setConfigValue(selected.getShortCode());
        facade.edit(moduleConfig);

        moduleConfig = facade.findByModuleAndKey(Constant.BROADCAST_MODULE_NAME, BroadcastConfig.SPEED_KEY);
        moduleConfig.setConfigValue(String.valueOf(selected.getSpeed()));
        facade.edit(moduleConfig);

        resetToCreateForm();
        JsfUtil.addSuccessMessage("Thành công!", "Đã cập nhật thành công");
    }

    @Override
    protected void removeSelected() throws Exception {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    protected void prepareViewAction() throws Exception {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    protected void prepareEditAction() throws Exception {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public BroadcastConfig getSelected() {
        return selected;
    }

    public void setSelected(BroadcastConfig selected) {
        this.selected = selected;
    }
}
